//
//  RootViewController.m
//  SunshineMusic
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 G.Group. All rights reserved.
//

#import "RootViewController.h"
#import "FirstPageViewController.h"
#import "LeftPageViewController.h"
#import <Masonry.h>

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define MAXOFFSET 200
#define ENDRIGHT 250
#define ENDLEFT 100

@interface RootViewController ()
@property (nonatomic, retain) FirstPageViewController *firstPageVC;
@property (nonatomic, retain) LeftPageViewController  *leftPageVC;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubViews];

}
- (void)createSubViews
{
    
    
    //添加左页面
    self.leftPageVC = [[LeftPageViewController alloc ] init];
    self.leftPageVC.view.frame  = self.view.frame;
    self.leftPageVC.view.backgroundColor = [UIColor redColor];
    [self addChildViewController:self.leftPageVC];
    [self.view addSubview:self.leftPageVC.view];
    
    //添加首页(右边页面)
    self.firstPageVC = [[FirstPageViewController alloc] init];
    [self addChildViewController:self.firstPageVC];
    self.firstPageVC.view.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.firstPageVC.view];
    self.firstPageVC.view.frame = self.view.frame;
    



 }
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    //获得当前点
    CGPoint currentPoint = [touch locationInView:self.firstPageVC.view];
    //获得起点
    CGPoint beginPoint = [touch previousLocationInView:self.firstPageVC.view];
    //获得点在x方向上的偏移量
    CGFloat moveX = currentPoint.x - beginPoint.x;
    
            self.firstPageVC.view.frame = [self frameWithOffsetX:moveX];

}
- (CGRect)frameWithOffsetX:(CGFloat)offsetX
{
    //计算在y方向上的偏移量
    CGFloat offsetY = offsetX / WIDTH * MAXOFFSET;
    //根据y方向上的偏移量计算缩放比例
    CGFloat scale = (HEIGHT - 2 * offsetY) / HEIGHT;
    //如果x < 0 表示左滑
    if (self.firstPageVC.view.frame.origin.x < 0) {
        scale = (HEIGHT + 2 * offsetY) / HEIGHT;
    }
    CGRect frame = self.firstPageVC.view.frame;
    //计算滑动之后的frame
    CGFloat height = frame.size.height * scale;
    CGFloat width = frame.size.width;
    CGFloat x = frame.origin.x + offsetX;
    CGFloat y = (HEIGHT - height) * 0.5;
    
    return CGRectMake(x, y, width, height);
}



- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGFloat xPos = _firstPageVC.view.frame.origin.x;
    if (xPos > WIDTH * 0.5) {
        [UIView animateWithDuration:0.5 animations:^{
            self.firstPageVC.view.frame = [self frameWithBigThanX:ENDRIGHT];
            
        }];
        return;
    }
    if (xPos < WIDTH * 0.5 && xPos > -WIDTH * 0.5) {
        [UIView animateWithDuration:0.5 animations:^{
            
              self.firstPageVC.view.frame = [UIScreen mainScreen].bounds;
        }];
           return;
    }
    [UIView animateWithDuration:0.5 animations:^{
        self.firstPageVC.view.frame =  [self frameWithSmallThanX:ENDLEFT];
    }];
 
}
- (CGRect)frameWithBigThanX:(CGFloat)offsetX
{
    CGFloat offsetY = offsetX / WIDTH * MAXOFFSET;
    CGFloat scale = (HEIGHT - 2 * offsetY) / HEIGHT;
    CGFloat height = HEIGHT * scale;
    CGFloat width = WIDTH;
    CGFloat x = offsetX;
    CGFloat y = (HEIGHT - height) * 0.5;
    
    return CGRectMake(x, y, width, height);
}
- (CGRect)frameWithSmallThanX:(CGFloat)offsetX
{
    CGFloat offsetY = offsetX / WIDTH * MAXOFFSET;
    CGFloat scale = (HEIGHT + 2 * offsetY) / HEIGHT;
    CGFloat height = HEIGHT * scale;
    CGFloat width  = WIDTH;
    CGFloat x = offsetX;
    CGFloat y = (HEIGHT - height)* 0.5;
    
    return CGRectMake(x, y, width, height);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
